<?php
require_once 'Zend/Controller/Action.php';
class MatchController extends App_Zend_Controller_Action_Shop
{
    public function indexAction()
    {
    	$this->getHelper('Fenlei')->getCategories();
    	$this->view->styleName = 'jc';
    	//$this->getHelper('layout')->disableLayout();
    	$member=new CMember();
        if(!$member->isUser($this->getRequest()->getModuleName(),$this->getRequest()->getControllerName())||$_COOKIE[COOKIE_PRE.'member_type']!='buyer') {
            // $this->getHelper('viewRenderer')->setNoRender();
        	//$this->view->showMsg('Please Login First', '/auth-login?preurl=/match');
        }
        
        $matchId = $this->getRequest()->getParam('matchid');
        $matchId = intval($matchId);
        
        $memberInfo = $member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE . 'uid']));
    	$pageNum = 1;
    	$page = $this->getRequest()->getParam('page');
    	$page = intval($page);
    	
    	if ($page == 0) {
    		$page = 1;
    	}
    	
    	$matchInfo = new CMatchinfo();
    	
    	if ($matchId) {
    		$matchList = $matchInfo->find($matchId);
    	} else {
    		$matchList = $matchInfo->fetchAllMatch($page, $pageNum, 0);
    		$pageCount = $matchInfo->getPageCount($pageNum, 0);
    	
	    	if ($page < 1 ) {
	    		$page = 1;
	    	} elseif ($page > $pageCount) {
	    		$page = $pageCount;
	    	}
	    	$pageString = CPage::divPages($pageCount, $page, ROOT_PATH."/match-index?");
    	}
    	
    	$match = $matchList->current();
    	
    	if (empty($match)) {    		
    		$this->getHelper('viewRenderer')->setNoRender();
    		$this->view->showMsg('No Match Now', '/');  
    		return;  		
    	}

    	$teamA = $matchInfo->getTeam($match->TeamA);
    	$teamB = $matchInfo->getTeam($match->TeamB);
    	$this->getHelper('layout')->getView()->headMeta()->setName('og:title', $teamA['en_flname'] . ' VS ' . $teamB['en_flname'] . ', guess who will win ,if you guess right, you can get money!');
		$this->getHelper('layout')->getView()->headMeta()->setName('og:type', 'jersery');
		$this->getHelper('layout')->getView()->headMeta()->setName('og:image', MY_URL . '/' . $match->MatchLogo);
		$this->getHelper('layout')->getView()->headMeta()->setName('og:url', MY_URL . '/match-index?matchid=' . $match->MatchID);		
		$this->getHelper('layout')->getView()->headMeta()->setName('og:site_name', 'newjerseysoccer');
		$this->getHelper('layout')->getView()->headMeta()->setName('og:description', $teamA['en_flname'] . ' VS ' . $teamB['en_flname'] . ', guess who will win ,if you guess right, you can get money!');
    	
    	
    	$fenlei = new CFenlei('product');
    	$pid = 92;
    	$topClub = $fenlei->getAllFenleiByPid(0, 3, $pid);
    	$topClubMore = $fenlei->getAllFenleiByPid(0, 10, $pid);
    	
    	$oldMatchList = $matchInfo->fetchAllMatch(1, 6, -2);    	
    	
    	$member = new CMember();
    	$buyer = new CBuyer();
    	$userArray = $member->getAllUsersSort(0, 9, array('member_type'=>'buyer'));
    	$this->view->buyer = $buyer;
    	$this->view->userArray = $userArray;
    	
    	$score = new CScore();
    	$this->view->score = $score;
    	
    	$this->view->webSiteTitle = $teamA['en_flname'] . ' VS ' . $teamB['en_flname'];
    	$this->view->webSiteKeywords = $teamA['en_flname'] . ' VS ' . $teamB['en_flname'];
    	$this->view->webSiteDescription = $teamA['en_flname'] . ' VS ' . $teamB['en_flname'];
    	
    	$this->view->topClub = $topClub;
    	$this->view->topClubMore = $topClubMore;
    	
    	$this->view->member = $member;
    	$this->view->matchList = $matchList;
    	$this->view->matchInfo = $matchInfo;
    	$this->view->pageString = $pageString;
    	$this->view->memberInfo = $memberInfo;
    	$this->view->oldMatchList = $oldMatchList;
    }
    
    public function clubAction()
    {
    	//$this->getHelper('layout')->disableLayout();
    	$page = $this->_request->getParam("page", 1);
		$pid = 92;
		$fenlei = new CFenlei('product');
		
    	$fenleiCounts = $fenlei->getAllFlCountsByPid($pid);
        Zend_Loader::loadClass("CFilter");
        $pageArray = CFilter::filterPage($page, $fenleiCounts, 10);
        $fenleiArray = $fenlei->getAllFenleiByPid($pageArray['from'], 10, $pid);
        Zend_Loader::loadClass("CPage");
        $pageString = CPage::divPages($pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH . "/match-club?"); //生成分页字符串
        $this->view->assign("pageString", $pageString);
        $this->view->assign("pageArray", $pageArray);
        $this->view->assign('fenleiArray', $fenleiArray);
        $this->view->start = ($page - 1) * 10;
    }
    
    public function userAction()
    {
    	//$this->getHelper('layout')->disableLayout();
    	$page = $this->_request->getParam("page", 1);
    	
    	$member = new CMember();
    	$buyer = new CBuyer();
    	$userCounts = $member->getUserCounts(array('member_type'=>'buyer'));
    	Zend_Loader::loadClass("CFilter");
        $pageArray = CFilter::filterPage($page, $userCounts, 10);
        $userArray = $member->getAllUsersSort($pageArray['from'], 10, array('member_type'=>'buyer'));
        Zend_Loader::loadClass("CPage");
        $pageString = CPage::divPages($pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH . "/match-user?"); //生成分页字符串
        $this->view->assign("pageString", $pageString);
        $this->view->assign("pageArray", $pageArray);
        $this->view->assign('userArray', $userArray);
        $this->view->buyer = $buyer;
        $this->view->start = ($page - 1) * 10;
    	
    }
    
    public function chipinAction()
    {
    	$this->getHelper('layout')->disableLayout();
    	
    	$member=new CMember();
        if(!$member->isUser($this->getRequest()->getModuleName(),$this->getRequest()->getControllerName())||$_COOKIE[COOKIE_PRE.'member_type']!='buyer') {
            echo '0'; //Please Login First!
            exit();
        }
        
        $uid = $_COOKIE[COOKIE_PRE . 'uid'];
        
        if ($this->getRequest()->isPost()) {
	        $score = $this->getRequest()->getParam('score');
	    	$matchId = $this->getRequest()->getParam('matchid');
	    	$teamId = $this->getRequest()->getParam('teamid');
	    	
	    	$matchId = intval(trim($matchId));
	    	$teamId = intval(trim($teamId));
	    	$score = intval(trim($score));
	    	
	    	$scoreTable = new CScore();
	    	$score = $scoreTable->dollar2score($score);
	    	
	    	$matchInfo = new CMatchinfo();	    	
	    	$rate = $matchInfo->getRate($matchId, $teamId);    	
	    	if (!$rate) {
	    		echo 'error';
	    		exit();
	    	}
	    	
	    	$user = $member->getUserDetails(array('uid'=>$uid));
	    	if ($user[0]['integral'] < $score) {
	    		echo '1'; //Your Score Is Not Enough
	    		exit();
	    	}
	    	
	    	$match = $matchInfo->fetchRow(array('MatchID = ?'=>$matchId));
	    	if ($match->MatchStatus != 0) {
	    		echo 'error';
	    		exit();
	    	}	    	
	    	if ($match->TeamA == $teamId) {
	    		$team = 'ChipInA';
	    	} elseif ($match->TeamB == $teamId) {
	    		$team = 'ChipInB';
	    	} else {
	    		echo 'error';
	    		exit();
	    	}
	    	
	    	$row = array('UID'=>$uid, 'MatchID'=>$matchId, 'RateID'=>$rate['MatchRateID'], 'ChipInScore'=>$score, 'ChipIndate'=>time());
	    	if ($matchInfo->addChipin($row, $member, $team)) {
	    		echo '2'; //Chipin Success
	    	} else {
	    		echo '3'; //Chipin Faliure
	    	} 
        }   	
    }
    
    public function commentAction()
    {
    	$this->getHelper('layout')->disableLayout();
    	$this->getHelper('viewRenderer')->setNoRender();
    	
    	$member=new CMember();
        if(!$member->isUser($this->getRequest()->getModuleName(),$this->getRequest()->getControllerName())||$_COOKIE[COOKIE_PRE.'member_type']!='buyer') {
            echo '0';
            exit();
        }
        
        $matchID = intval($this->getRequest()->getParam('matchid'));
        $matchComment = new CMatchcomments();
        
    	if ($this->getRequest()->isPost()) {
	        $uid = $_COOKIE[COOKIE_PRE . 'uid'];
	        $ip = CString::getIp();
	        $content = $this->getRequest()->getParam('content');
	        $content = $this->view->escape($content);
	        if (empty($content)) {
	        	echo '1';
	        	return;
	        }
	        $row = array('uid'=>$uid, 'MatchID'=>$matchID, 'content'=>$content, 'addtime'=>time(), 'ip'=>$ip);
	        if ($matchComment->add($row)) {
	        	echo '2';
	        } else {
	        	echo '3';
	        }
        }
    }
    
    public function getcommentAction()
    {
    	$this->getHelper('layout')->disableLayout();
    	$matchID = intval($this->getRequest()->getParam('matchid'));
        $matchComment = new CMatchcomments();
        
        $page = $this->getRequest()->getParam('page');
        $page = intval($page);
        
        $pageNum = 6;
        $pageCount = $matchComment->getPageCount($pageNum, $matchID);
        
        if ($page < 1) {
        	$page = 1;
        } elseif ($page > $pageCount) {
        	$page = $pageCount;
        }
        $buyer = new CBuyer();
        $pageString = CPage::divPages($pageCount, $page, ROOT_PATH."/match-comment?matchid=$matchID");
        $commentList = $matchComment->fetchAllComment($matchID);
        
        $this->view->member = new CMember();
        $this->view->commentList = $commentList;
        $this->view->buyer = $buyer;
        $this->view->matchID = $matchID;     
        $this->view->pageString = $pageString;   
    }
    
    public function commentdetailAction()
    {
    	$commentId = $this->getRequest()->getParam('commentid');
    	$commentId = intval($commentId);
    	
    	$comment = new CMatchcomments();
    	$commentInfo = $comment->find($commentId)->current();
    	
    	$match = new CMatchinfo();
    	$matchInfo = $match->find($commentInfo->MatchID)->current();
    	
    	$buyer = new CBuyer();
    	
    	$this->view->buyer = $buyer;
    	$this->view->commentInfo = $commentInfo;
    	$this->view->matchInfo = $matchInfo;
    	$this->view->match = $match;
    	
    	$team = $match->getTeam($matchInfo->TeamA);
    	
    	$this->getHelper('layout')->getView()->headMeta()->setName('og:title', mb_substr($commentInfo->content, 0,200));
		$this->getHelper('layout')->getView()->headMeta()->setName('og:type', 'jersery');
		$this->getHelper('layout')->getView()->headMeta()->setName('og:image', MY_URL . $team['logostr']);
		$this->getHelper('layout')->getView()->headMeta()->setName('og:url', MY_URL . '/match-commentdetail?commentid=' . $commentInfo->matchcomments_id);		
		$this->getHelper('layout')->getView()->headMeta()->setName('og:site_name', 'newjerseysoccer');
		$this->getHelper('layout')->getView()->headMeta()->setName('og:description', $commentInfo->content);
    }
    
    public function getscoreAction()
    {
    	$this->getHelper('viewRenderer')->setNoRender();
    	$this->getHelper('layout')->disableLayout();
    	$member = new CMember();
    	$memberInfo = $member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE . 'uid']));
    	$score = new CScore();
    	if ($memberInfo) {
    		echo $score->score2dollar($memberInfo[0]['integral']);
    	} else {
    		echo 0;
    	}
    }
} 